import axios,{InternalAxiosRequestConfig,AxiosError,AxiosResponse} from 'axios'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
NProgress.configure({
    easing: "ease-in-out",
    speed: 300
 })
const instance=axios.create({
    baseURL:import.meta.env.VITE_BASE_URL,
    timeout:5000
})
instance.interceptors.request.use((config:InternalAxiosRequestConfig)=>{
    NProgress.start()
    const user=JSON.parse(localStorage.getItem('user')!)
    if(user){
        config.headers['Authori-zation']=user.token
    }
    return config
},(err:AxiosError)=>{
    return Promise.reject(err)
})
instance.interceptors.response.use((response:AxiosResponse)=>{
    NProgress.done()
    return response
},(err:AxiosError)=>{
    if(err.response){
        switch(err.response.status){
            case 404:
                break
            case 401:
                break
            case 500:
                break
        }
    }
    return Promise.reject(err)
})
export default instance